<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>排序配置</title>
        <{$showjs}>
        <{$ajax}>
        <link rel="stylesheet" type="text/css" href="../css/main1.css" />
        <style type=text/css>
               <!--
               .x-btn-left{
	background-position:2px 0;
               }
               .x-btn-over .x-btn-left{
	background-position:2px -63px;
               }
               .i_sx{background-image:url(../img/arrow_refresh_small.png) !important;}
               .i_cs{background-image:url(../img/application_form_add.png) !important;}
               .i_sc{background-image:url(../img/application_form_delete.png) !important;}
               .i_qk{background-image:url(../img/application_delete.png) !important;}
               .i_px{background-image:url(../img/sucaiwcom8170000bzc.gif) !important;}

        --> </style>
        <script type="text/javascript" src="/Plug-in/ext/Ext.grid.CheckColumn.js"></script>
        <script type="text/javascript">

            Ext.onReady(function(){
                var myMask = new Ext.LoadMask(Ext.getBody(), {
                    msg: '请稍候...'
                });
                var fm = Ext.form;
                var sm = new Ext.grid.CheckboxSelectionModel();
                var fields = [
                    sm,
                    {name: 'id', mapping : 'id'},
                    {name: 'tt', mapping : 'tt'},
                    {name: '<{$zd}>', mapping : '<{$zd}>'}
                ];
                var cm_config = [];
                var showField = '';     //显示的字段,这个在px.php里面处理
                var getField = '';     //搜索的字段,这个在px.php里面处理
                Ext.Ajax.request({
                    waitMsg:'数据加载中...',
                    url:'./px.php?op=getOrderField',
                    method:'POST',
                    params:{
                        tid:'<{$tid}>',
                        lg:'<{$lg}>',
                        orderField:'<{$zd}>'
                    },
                    success:function(response){
                        var result = Ext.util.JSON.decode(response.responseText);
                        fields = eval('['+result.fields+']');
                        cm_config = eval('['+result.cm+']');
                        showField = result.showField;
                        getField = result.getField;
                        orderInt();
                        
                    },
                    failure:function(response){
                        Ext.MessageBox.alert('警告',"无法连接数据库!");
                        window.location.refresh();
                    }
                });
                
                
                function orderInt(){
                    var row_id=0;

                    var store =  new Ext.data.JsonStore({
                        id:'store',
                        url: 'px.php',
                        fields: fields,
                        listeners :{
                            load :function(){
                                //alert('a')
                            }
                        }
                    });
                    store.load({params:{op: 'showall',tid:'<{$tid}>',getField: getField,bdid:'<{$smarty.get.bdid}>', zd: '<{$zd}>',lg:'<{$lg}>'}});

                    var cm = new Ext.grid.ColumnModel(
                    cm_config
                    /*
                    [
                        {
                            id : 'id',
                            header: "唯一编号",
                            dataIndex: 'id',
                            width: 80

                        },{
                            id : 'tt',
                            header: "排序标识",
                            dataIndex: 'tt',
                            width: 168
                        },{
                            id : '<{$zd}>',
                            header: "位置（倒序数字越大越靠前）",
                            dataIndex: '<{$zd}>',
                            width: 168,sortable:true,
                            editor: new fm.TextField({allowBlank: false})
                        }
                    ]
                    */
                    );

                    var grid = new Ext.grid.EditorGridPanel({
                        id				 :'grid',
                        store            : store,
                        cm				 : cm,
                        sm				 : sm,
                        region           : 'center',
                        frame			 : false,
                        clicksToEdit	 : 1,
                        ddGroup			 :'GridDD',
                        enableDragDrop 	 : false,
                        border			 : false,
                        title            : false,
                        tbar:[{xtype: 'button',text:'<{$tid}>_<{$zd}>的排序',iconCls:'i_tablecfg'}],
                        listeners: {
                            'render': function(){
                                bbar2.render(this.tbar);
                                //cshzdpx();
                            },
                            "beforeedit":{
                                fn: function(items){
                                    row_id=items.row;
                                },
                                scope: this
                            },
                            "afterEdit": {
                                fn: function(items){
                                    myMask.show();
                                    Ext.Ajax.request(
                                    {
                                        waitMsg:'正在提交....',
                                        method:'POST',
                                        params:{op:'items_config_save_ingrid',id:items.record.data.id,value:items.value,field:items.field,table:'<{$tablename2}>'},
                                        url:'l.php',
                                        success:function(form, action) {
                                            store.reload();
                                            myMask.hide();
                                        },
                                        failure: function(form, action) {
                                        }
                                    }
                                );
                                },
                                scope: this
                            }
                        }
                    });

                    /**
                     * 初始化字段排序
                     */
                    function cshzdpx(){
                        myMask.show();
                        Ext.Ajax.request({
                            waitMsg:'正在提交....',
                            method:'POST',
                            params:{op:'pxcs',tid:"<{$tid}>",getField:getField,zd:'<{$zd}>',lg:'<{$lg}>'},
                            url:'px.php',
                            success:function(form, action) {
                                store.reload();
                                myMask.hide();
                            },
                            failure: function(form, action) {
                            }
                        });
                    }

                    bbar2 = new Ext.Toolbar({
                        renderTo:grid.tbar
                        ,items:['->',{
                                text:'刷新',iconCls:'i_sx',handler:function(){
                                    myMask.show();
                                    store.reload();
                                    myMask.hide();
                                }
                            }/*,'-',{
                                text:'初始化字段排序',iconCls:'i_px',handler:function(){
                                    cshzdpx();
                                }
                            }*/,'-',{
                                text:'删除',iconCls:'i_sc',
                                handler:function(){
                                    myMask.show();
                                    var selections = grid.selModel.getSelections();
                                    var prez = [];
                                    for(i = 0; i< grid.selModel.getCount(); i++){
                                        prez.push(selections[i].json.id);
                                    }
                                    var encoded_array= Ext.encode(prez);
                                    Ext.Ajax.request(
                                    {
                                        waitMsg:'正在提交....',
                                        method:'POST',
                                        params:{op:'del',ids:encoded_array,tid:"<{$tid}>",zd:"<{$zd}>"},
                                        url:'px.php',
                                        success:function(form, action) {
                                            store.reload();
                                            myMask.hide();
                                        },
                                        failure: function(form, action) {
                                        }
                                    }
                                );
                                }
                            }
                        ]
                    });
                    var viewport = new Ext.Viewport({
                        layout : 'fit',
                        items:[
                            grid
                        ]
                    });
                    var ddrow = new Ext.dd.DropTarget(grid.getView().mainBody, {
                        ddGroup : 'GridDD',
                        copy:false,
                        notifyDrop : function(dd, e, data) {

                            var sm = grid.getSelectionModel();
                            var rows = sm.getSelections();
                            var param =rows[0].data;
                            //alert(param.id);
                            var cindex = dd.getDragData(e).rowIndex;
                            //alert(dd.getDragData(e).selections['id']);
                            //			alert(cindex);
                            myMask.show();
                            Ext.Ajax.request(
                            {
                                waitMsg:'正在提交....',
                                method:'POST',
                                params:{op:'px',nowid:param.id,wz:cindex,tid:"<{$tid}>",zd:"<{$zd}>",lg:'<{$lg}>'},
                                url:'px.php',
                                success:function(form, action) {
                                    store.reload();
                                    myMask.hide();
                                },
                                failure: function(form, action) {
                                }
                            }
                        );
                        }
                    });
                }
            });
        </script>
    </head>
    <body>

    </body>
</html>